package com.example.houduan.Controller;

import com.example.houduan.Pojo.Sale;
import com.example.houduan.Service.SaleService;
import com.example.houduan.Utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
@CrossOrigin
public class SaleController {

    @Autowired
    SaleService saleService;

    @RequestMapping("/getSalePage")
    @ResponseBody
    public PageModel getSalePage(@RequestParam int num,int size){
//        System.out.println(saleService.getSalePage(num, size));
        return saleService.getSalePage(num,size);
    }

    @RequestMapping("/deleteSale")
    @ResponseBody
    public String deleteSale(@RequestParam Integer sale_id){
        if(saleService.deleteSale(sale_id)==0){
            return "删除失败";
        }else {
            return "删除成功";
        }
    }

    @RequestMapping("/updateSale")
    @ResponseBody
    public String updateSale(@RequestBody Sale sale){
        sale.setUpdatetime(LocalDateTime.now());
        if(saleService.updateSale(sale)==0){
            return "修改失败";
        }else{
            return "修改成功";
        }
    }

    @RequestMapping("/addSale")
    @ResponseBody
    public String addSale(@RequestBody Sale sale){
        sale.setCreatetime(LocalDateTime.now());
        sale.setUpdatetime(LocalDateTime.now());
        if(saleService.addSale(sale)==0){
            return "添加失败";
        }else {
            return "添加成功";
        }
    }

    @RequestMapping("/getSaleByConditions")
    @ResponseBody
    public List<Sale> getSaleByConditions(@RequestParam(required = false) Integer sale_id,String sale_name,String city_name){
        System.out.println("sale_id="+sale_id+","+"sale_name="+sale_name+","+"city_name="+city_name);
        return saleService.getSaleByConditions(sale_id,sale_name,city_name);
    }
}
